import vue from '../main'
import store from '../store/index';
import { Notification  } from 'element-ui';
import { OBJ } from './init'
import { showMore, checkStr } from "./methods"
import { getWaypointByName, isNameUsedWaypoints } from "./waypoint"
import { navCtrl } from "./navigation" 

/**
 * 显示轨迹列表
 * flag :true显示  :false隐藏
 * **/
export function showTrajectoryList(flag=false){
    if(flag){
        store.commit('switchTrackList', true);
    }else{
        store.commit('switchTrackList', false);
    }
}

/**
 * 新建轨迹
 * flag :true显示  :false隐藏
 * 如果用户引导程序未结束，显示新建轨迹
 * **/
export function showTrajectoryAdd(flag=false){
    let guide = window.localStorage.getItem('guide');
    if(flag){
        store.commit('switchAddTrack',true);
        if(!guide){
            store.commit("guideIndex", 3);
        }
    }else{
        store.commit('switchAddTrack', false);
    }
}

/**
 * 显示轨迹详情
 * flag :true显示  :false隐藏
 * 匹配有无该轨迹,有则赋值
 * **/
export function showTrajectoryDetail(flag=false, name){
    if(flag){
        var tracks = getTrajectoryByName(name);
        console.log('[INFO]:' + JSON.stringify(tracks))
        if (tracks) {
            store.commit('setTrackDetail', tracks);
        } else {
            Notification({
                title: vue.$t("titles.error"),
                message: vue.$t("messages.notFindTrajectory") + name,
                type: 'error',
                position: 'top-left',
                offset: 100
            });
            console.warn(`[WARN]Can not find waypoint ${name}`);
            return
        }
        store.commit('switchTrackModule', true);
    }else{
        store.commit('switchTrackModule', false);
    }
}
    
/**
 * 点击导航指定轨迹
 * **/
export function navTrack(name) {
    var trajInfo = getTrajectoryByName(name);
    navCtrl(trajInfo.name, 1);
};


/**
 * 确定删除轨迹
 * **/
export function delTrack(fullName) {
    var traj = getTrajectoryByName(fullName);
    var msg = new ROSLIB.Message(traj);
    console.log(`delete Trajectory Name:${fullName}`)
    OBJ.topic.delTrajTopic.publish(msg);

    Notification({
        title: vue.$t("titles.success"),
        message: vue.$t("messages.trajectoryDelSuccess"),
        type: 'success',
        position: 'top-left',
        offset: 100
    });
};

/**
 * 新建轨迹
 * 提交
 * **/
export function submitAddTrack(infos) {
    if (infos.name === "") {
        Notification({
            title: vue.$t("titles.warning"),
            message: vue.$t("messages.refineTrajecName"),
            type: 'warning',
            position: 'top-left',
            offset: 100
        });
        return
    }
    if (!checkStr(infos.name)) {
        Notification({
            title: vue.$t("titles.warning"),
            message: vue.$t("messages.theNameUnavailable"),
            type: 'warning',
            position: 'top-left',
            offset: 100
        });
        return;
    }
    if (infos.stations.length === 0) {
        Notification({
            title: vue.$t("titles.warning"),
            message: vue.$t("messages.minSelectWaypoint"),
            type: 'warning',
            position: 'top-left',
            offset: 100
        });
        return
    }
    if (isNameUsedtraject(infos.name)) {
        Notification({
            title: vue.$t("titles.warning"),
            message: vue.$t("messages.trajectoryExists"),
            type: 'warning',
            position: 'top-left',
            offset: 100
        });
        return
    }

    if (isNameUsedWaypoints(infos.name)) {
        Notification({
            title: vue.$t("titles.warning"),
            message: vue.$t("messages.trajectoryWithWaypointExists"),
            type: 'warning',
            position: 'top-left',
            offset: 100
        });
        return;
    }

    //引导流程为轨迹
    window.localStorage.setItem('guideName', 'trajectory');

    let stationsName = infos.stations;
    let waypoints = [];
    for (let i = 0; i < stationsName.length; i++) {
        let selectedWpName = stationsName[i];
        waypoints.push(getWaypointByName(selectedWpName));
    }
    let msg = new ROSLIB.Message({
        name: infos.name,
        waypoints: waypoints
    });
    OBJ.topic.addTrajectoryTopic.publish(msg);
    Notification({
        title: vue.$t("titles.success"),
        message: vue.$t("messages.trajectoryAddSuccess"),
        type: 'success',
        position: 'top-left',
        offset: 100
    });
    
    showTrajectoryAdd(false);
    //显示更多模态框
    let guide = window.localStorage.getItem('guide');
    if(!guide){
        showMore(true);
    }
};

/**
 * 通过名称获取对应的轨迹数据
 * **/
export function getTrajectoryByName(name) {
    var traj;
    for (var i = 0; i < store.state.trajectoryList.length; i++) {
        traj = store.state.trajectoryList[i];
        if (traj.name === name) {
            break;
        }
    }
    return traj;
};

/**
 * 轨迹名字是否重复
 * **/
export function isNameUsedtraject(name) {
    if (store.state.trajectoryList) {
        for (var i = 0; i < store.state.trajectoryList.length; i++) {
            if (store.state.trajectoryList[i].name === name) {
                return true;
            }
        }
    }
    return false;
};